package com.duobaoyu.dby.supply.biz.client.boundary.order.bean.bo;

import cn.hutool.core.util.StrUtil;
import com.duobaoyu.dby.channel.adapter.enums.biz.order.OrderStatusEnum;
import com.duobaoyu.dby.common.core.constant.StringPool;
import lombok.Data;
import org.apache.commons.lang3.builder.DiffBuilder;
import org.apache.commons.lang3.builder.DiffResult;
import org.apache.commons.lang3.builder.Diffable;
import org.apache.commons.lang3.builder.ToStringStyle;

import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * 订单信息
 * @Author yangyong
 * @Date 2023/10/18
 */
@Data
public class OrderInfoBO implements Serializable, Diffable<Object>{
	private static final long serialVersionUID = -8434203628620019827L;
	/**
	 * 主键ID
	 */
	private Long id;
	/**
	 * 渠道编码
	 */
	private String channelCode;
	/**
	 * 渠道子单号
	 */
	private String channelOrderSn;
	/**
	 * 渠道父单号
	 */
	private String channelOrderParentSn;
	/**
	 * 渠道交易单号
	 */
	private String channelTradeOrderSn;
	/**
	 * 渠道订单实际金额
	 */
	private BigDecimal channelOrderRealityAmount;
	/**
	 * 渠道订单总金额
	 */
	private BigDecimal channelOrderAmount;
	/**
	 * 渠道运费
	 */
	private BigDecimal channelFreightAmount;
	/**
	 * 订单总金额
	 */
	private BigDecimal orderAmount;
	/**
	 * 订单类型
	 */
	private String orderType;
	/**
	 * 业务订单交易号(与供应链订单号一一对应)
	 */
	private String bizTradeSn;
	/**
	 * 供应链交易单号(与业务订单号一一对应)
	 */
	private String supplyTradeSn;
	/**
	 * 供应链订单号
	 */
	private String orderSn;
	/**
	 * 供应链三方交易单号
	 */
	private String orderTradeSn;
	/**
	 * 供应链订单号父单号
	 */
	private String orderParentSn;
	/**
	 * 0:拆单前、1:拆单后
	 * @see com.duobaoyu.dby.supply.biz.enums.OrderParentTypeEnum
	 */
	private Integer parentType;
	/**
	 * 系统内部名称
	 */
	private String serverName;
	/**
	 * 展示来源渠道
	 */
	private String showServerName;
	/**
	 * 手机号/充值账号
	 */
	private String rechargeAccount;
	/**
	 * 下单失败原因
	 */
	private String failReason;
	/**
	 * 取消原因
	 */
	private String cancelReason;
	/**
	 * 备注信息
	 */
	private String orderRemark;
	/**
	 * 订单状态
	 */
	private Integer orderStatus;
	/**
	 * 订单标签: asyn-异步;sync-同步
	 */
	private String orderTag;
	/**
	 * 扩展字段
	 */
	private OrderExtendParamBO extendParam;
	/**
	 * 确认订单时间
	 */
	private LocalDateTime confirmOrderTime;
	/**
	 * 商户ID
	 */
	private String mchId;
	/**
	 * 用户ID
	 */
	private String userId;
	/**
	 * 状态变更时间
	 */
	private LocalDateTime statusChangeTime;
	/**
	 * 创建时间
	 */
	private LocalDateTime createTime;

	public String removeTag(String orderTag) {
		List<String> split = StrUtil.split(this.orderTag, StringPool.COMMA);
		if (Objects.isNull(split)) {
			return this.orderTag;
		}
		split.remove(orderTag);
		if (!split.isEmpty()) {
			this.orderTag = StrUtil.join(StringPool.COMMA, split);
		} else {
			this.orderTag = StringPool.EMPTY;
		}
		return this.orderTag;
	}

	@Override
	public DiffResult diff(Object obj){
		DiffBuilder diffBuilder = new DiffBuilder(this, obj, ToStringStyle.SHORT_PREFIX_STYLE);
		if (obj instanceof OrderBO) {
			OrderBO order = (OrderBO) obj;
			diffBuilder.append("订单状态", OrderStatusEnum.getOrderSubStatusDesc(this.orderStatus), OrderStatusEnum.getOrderSubStatusDesc(order.getOrderStatus()));
			diffBuilder.append("供应链三方交易单号", this.getOrderTradeSn(), order.getOrderTradeSn());
			diffBuilder.append("订单标签", this.getOrderTag(), order.getOrderTag());
			diffBuilder.append("渠道订单号", this.getChannelOrderSn(), order.getChannelOrderSn());
			diffBuilder.append("渠道交易号", this.getChannelTradeOrderSn(), order.getChannelTradeOrderSn());
		} else if (obj instanceof UpdateOrderBO) {
			UpdateOrderBO updateOrder = (UpdateOrderBO) obj;
			Optional.ofNullable(updateOrder.getOrderStatus()).ifPresent(item -> diffBuilder.append("订单状态", OrderStatusEnum.getOrderSubStatusDesc(updateOrder.getOrderStatus().getKey()), OrderStatusEnum.getOrderSubStatusDesc(updateOrder.getOrderStatus().getValue())));
			diffBuilder.append("订单标签", this.getOrderTag(), updateOrder.getOrderTag());
			diffBuilder.append("供应链三方交易单号", this.getOrderTradeSn(), updateOrder.getOrderTradeSn());
			diffBuilder.append("渠道订单号", this.getChannelOrderSn(), updateOrder.getChannelOrderSn());
			diffBuilder.append("渠道交易号", this.getChannelTradeOrderSn(), updateOrder.getChannelTradeOrderSn());
			diffBuilder.append("失败原因", this.getFailReason(), updateOrder.getErrorInfo());
		}
		return diffBuilder.build();
	}
}
